package com.cms.server.resolver.plugins;

import java.sql.ResultSet;
import java.util.Date;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

public class CMSReTakeApprovalPSHTForm extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void saveBillData(JStatement stmt, EFFormDataModel formDataModel, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = formDataModel.getBillDataSet();
		String                            strSql = "";
		ResultSet                             rs = null;
		long                             curTime = new Date().getTime();
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           FLOW_ID = PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "");
		String                             OP_ID = PO.GetValueByParamName(FLOW_TASK_LIST._OP_ID, "");
		
		String                          NODE_SRC = PO.GetValueByParamName(FLOW_TASK_LIST._NODE_SRC, "");
		String                          NODE_TAG = PO.GetValueByParamName(FLOW_TASK_LIST._NODE_TAG, "");
		
		//如果不是开始节点，则需要更新上一个流程节点任务信息，同时还要插入一条新的记录
		strSql = "select * from FLOW_TASK_LIST where FLOW_ID = '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "' and BIZ_DATE = '" + F_KJQJ + "' and "
        	   + " BIZ_GUID = '" + F_GUID + "' and BIZ_DJBH ='" + F_DJBH + "' and OP_ID = '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_ID, "") + "' and "
        	   + " FLOW_ID = '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "' order by (OP_ID + 0) desc";
		
		rs = stmt.executeQuery(strSql);
		
		if(rs != null && rs.next()) {
			NODE_SRC = rs.getString("NODE_SRC");
			NODE_TAG = rs.getString("NODE_TAG");
			
			//更新上一个任务节点的状态
			strSql = "update FLOW_TASK_LIST set RESR_STATUS = 'processed', RESR_OUT_CAUSE = '" 
				   + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "") + "' where OP_ID = '" 
				   + OP_ID + "' and FLOW_ID = '" + FLOW_ID + "'";
			stmt.execute(strSql);
			
			//部门确认的时候时候退回，但是只能退回一次
			if(NODE_SRC.equals("N3") && NODE_TAG.equals("N4")) {
				//
		    	strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
					   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS," 
					   + "RESR_IN_CAUSE,RESR_OUT_CAUSE,TASK_TO_UNIT,";
		    	for(int j = 1; j <= 20; j++) {
		    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
		    			if(j < 10) {
			    			strSql += "F_CHAR0" + j + ",";
			    		} else {
			    			strSql += "F_CHAR" + j + ",";
			    		}
		    		}
		    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
		    			if(j < 10) {
			    			strSql += "F_DATE0" + j + ",";
			    		} else {
			    			strSql += "F_DATE" + j + ",";
			    		}
		    		}
		    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
		    			if(j < 10) {
			    			strSql += "F_NUM0" + j + ",";
			    		} else {
			    			strSql += "F_NUM" + j + ",";
			    		}
		    		}
		    	}
		    	strSql += "TASK_TO_UNIT_NAME) values ('" 
					    + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "', '"
					    + F_DJBH + "', '" + F_KJQJ + "', '" + F_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
					    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
					    + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '', 'processing', '"
					    + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '"
					    + PO.GetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._NODE_TAG, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._NODE_TAG_NAME, "") 
					    + "', '" + OP_ID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "") 
					    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "") + "', '', '" + PO.GetValueByParamName(FLOW_TASK_LIST._TASK_TO_UNIT, "") + "',";
		    	for(int j = 1; j <= 20; j++) {
		    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
		    			if(j < 10) {
			    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
			    		} else {
			    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) + "',";
			    		}
		    		}
		    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
		    			if(j < 10) {
			    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null)  + "',";
			    		} else {
			    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) + "',";
			    		}
		    		}
		    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
		    			if(j < 10) {
			    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null)  + "',";
			    		} else {
			    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) + "',";
			    		}
		    		}
		    		
		    	}
		    	strSql += "'')";
				stmt.execute(strSql);
				PO.SetValueByParamName("OP_ID", String.valueOf(curTime));
				return;
			}
			

			//
	    	strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
				   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS," 
				   + "RESR_IN_CAUSE,RESR_OUT_CAUSE,TASK_TO_UNIT,";
	    	for(int j = 1; j <= 20; j++) {
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
	    			if(j < 10) {
		    			strSql += "F_CHAR0" + j + ",";
		    		} else {
		    			strSql += "F_CHAR" + j + ",";
		    		}
	    		}
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
	    			if(j < 10) {
		    			strSql += "F_DATE0" + j + ",";
		    		} else {
		    			strSql += "F_DATE" + j + ",";
		    		}
	    		}
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
	    			if(j < 10) {
		    			strSql += "F_NUM0" + j + ",";
		    		} else {
		    			strSql += "F_NUM" + j + ",";
		    		}
	    		}
	    	}
	    	strSql += "TASK_TO_UNIT_NAME) values ('" 
				    + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "', '"
				    + F_DJBH + "', '" + F_KJQJ + "', '" + F_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
				    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
				    + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '', 'processing', '"
				    + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '"
				    + PO.GetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._NODE_TAG, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._NODE_TAG_NAME, "") 
				    + "', '" + OP_ID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "") 
				    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "") + "', '', '" + PO.GetValueByParamName(FLOW_TASK_LIST._TASK_TO_UNIT, "") + "',";
	    	for(int j = 1; j <= 20; j++) {
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
	    			if(j < 10) {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
		    		} else {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) + "',";
		    		}
	    		}
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
	    			if(j < 10) {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null)  + "',";
		    		} else {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) + "',";
		    		}
	    		}
	    		if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
	    			if(j < 10) {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null)  + "',";
		    		} else {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) + "',";
		    		}
	    		}
	    		
	    	}
	    	strSql += "'')";
			stmt.execute(strSql);
			PO.SetValueByParamName("OP_ID", String.valueOf(curTime));
		}
	}
}
